package com.zeny.express.framework.webapp.grid;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeny.express.framework.core.service.BaseService;
import com.zeny.express.framework.shared.domain.Constants;
import com.zeny.express.framework.shared.util.UUIDUtils;

@Service
public class GridColumnServiceImpl extends BaseService implements GridColumnService {

	@Autowired
	private GridColumnEntityDao gridColumnEntityDao;
	
	@Override
	public GridColumnEntity query(String gridId, String userCode) {
		
		if (StringUtils.isEmpty(gridId) || StringUtils.isEmpty(userCode)) {
			return null;
		}
		
		GridColumnEntity condition = new GridColumnEntity();
		condition.setActive(Constants.YES);
		condition.setGridId(gridId);
		condition.setUserCode(userCode);
		
		return gridColumnEntityDao.selectByCondition(condition);
	}

	@Override
	public void save(String gridId, String userCode, String content) {
		
		GridColumnEntity entity = query(gridId, userCode);
		
		if (entity == null) {
			
			entity = new GridColumnEntity();
			
			entity.setId(UUIDUtils.getTimeUUID());
			entity.setActive(Constants.YES);
			entity.setGridId(gridId);
			entity.setUserCode(userCode);
			entity.setContent(content);
			entity.setCreateTime(new Date());
			
			gridColumnEntityDao.insert(entity);
			
		} else {
			entity.setContent(content);
			
			gridColumnEntityDao.updateById(entity);
		}
		
	}

	@Override
	public void delete(String gridId, String userCode) {

		if (StringUtils.isEmpty(gridId) || StringUtils.isEmpty(userCode)) {
			return;
		}
		
		GridColumnEntity entity = query(gridId, userCode);
		
		if (entity != null) {
			gridColumnEntityDao.deleteById(entity.getId());
		}
		
	}

}
